*****************************************************************************
*			          Analysis Speech by MSA in the US	       				*
*																			*			
* Author: 			Valentina Gonzalez Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			August 9 2024   										*
* Version:			Stata 17 												*                                                                          
*																			*
*****************************************************************************
/*
This do-file:
- Creates Table 2 and A15 using data from Trump Speeches. 

Input:
- Data\Text\combined_df.csv // This file contains the data from speeches
- Data\Rally_Visits_MSA.dta // This file contains information about the MSA (e.g, number of exposed workers)
- Alternatively you can go to line 45 and use prepared data: 
	- Data\Speech_MSA.dta

Output:
- Table 2: Trump's Campaign Strategy: Speeches [Table\Trump_text_IVchanged.tex]
- Table A15: Trump's Campaing Strategy: Speeches (Total count) [Table\Trump_text_IVchanged_count.tex]

*/

*Defining Directory
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"

* Merging MSA data with speeches - alternatively go to line 45
{
import delimited "Data\Text\combined_df.csv", clear
merge m:m msa_state using "Data\Rally_Visits_MSA.dta"
keep if _merge==3
drop _merge 

// Generating the variables of interest regarding exposure of workers and hate crimes
 
gen high_pop_pop=(high_pop/Population) // Share of exposed workers 
gen anti_pop=(anti/Population)*100000 

keep state  word_count  msa_state  pro_worker_count  culture_count  veryclose10 foreign  month  high_pop_pop   anti_pop 
save "Data\Speech_MSA.dta", replace

}

* Alternatively you can call directly the data
use "Data\Speech_MSA.dta", clear 

*******************************************************************************
* Preparing variables
*******************************************************************************
{
encode state, generate(state_num2)  // Encode 'state' as numeric
encode msa_state, generate(msa_num)  // Encode 'msa_state' as numeric

// Calculate word shares
gen pro_w = pro_worker_count / word_count  // Pro-worker word share
gen pro_c = culture_count / word_count  // Pro-culture word share

// Create interaction terms
gen int_exp_close = high_pop_pop * veryclose10  // Interaction: exposure x closeness
gen int_exp_anti = high_pop_pop * anti_pop  // Interaction: exposure x hate incidents

// Label variables
lab var veryclose10 "Close"  
lab var int_exp_close "Exposed x Close"
lab var int_exp_anti "Exposed x Hate"
lab var high_pop_pop "Workers Exp. to Auto."
lab var anti_pop "Hate Inc.x 100K Pop"

}
*****************************************
* Regression
*****************************************
{

//table 2: Trump's Campaign Strategy: Speeches
{
preserve  // Preserve the current dataset

keep if msa_num ~= .  // Keep observations with non-missing 'msa_num' (0 observations deleted)

eststo clear  // Clear any previously stored estimates

// DV: Share of pro-worker rhetoric
eststo: qui reg pro_w high_pop_pop veryclose10 i.month foreign anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg pro_w high_pop_pop veryclose10 int_exp_close  i.month foreign anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg pro_w high_pop_pop veryclose10 int_exp_close int_exp_anti i.month foreign anti_pop  i.state_num2,cluster(state_num2)

// DV: Share of pro-culture rhetoric
eststo: qui reg pro_c high_pop_pop veryclose10 i.month foreign anti_pop i.state_num2 ,cluster(state_num2)
eststo: qui reg pro_c high_pop_pop veryclose10 int_exp_close i.month foreign anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg pro_c high_pop_pop veryclose10 int_exp_close int_exp_anti i.month foreign anti_pop  i.state_num2,cluster(state_num2)

// Create regression table
esttab , replace label se ///
    title("Trump's Campaign Strategy \label {TableSpeech2}") ///
    compress nogap ///
    star(* 0.1 ** 0.05 *** 0.01) ///
    b(%6.3f) ///
    keep(high_pop* anti* *close* int*) ///
    scalars("N Observations" "r2 R$^2$" "aic AIC") ///
    indicate("FE State = *state*" "Foreign = foreign*" "FE Month = *month") ///
	nomtitle collabels(none) mgroups("Pro-worker Rhetoric (1-3)" "Cultural Rhetoric (4-6)", pattern(1 0 0 1 0 0)  ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span)
	
// Create regression table
esttab using "Table\Trump_text_IVchanged.tex", replace label se ///
    title("Trump's Campaign Strategy \label {TableSpeech2}") ///
    compress nogap ///
    star(* 0.1 ** 0.05 *** 0.01) ///
    b(%6.3f) ///
    keep(high_pop* anti* *close* int*) ///
    scalars("N Observations" "r2 R$^2$" "aic AIC") ///
    indicate("FE State = *state*" "Foreign = foreign*" "FE Month = *month") ///
	nomtitle collabels(none) mgroups("Pro-worker Rhetoric (1-3)" "Cultural Rhetoric (4-6)", pattern(1 0 0 1 0 0)  ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span)


// Save regression table to a .tex file


restore  // Restore the original dataset

/////
}

// table A15: Trump's Campaing Strategy: Speeches (Total count)
{
preserve  // Preserve the current dataset

keep if msa_num ~= .  // Keep observations with non-missing 'msa_num' (0 observations deleted)

eststo clear  // Clear any previously stored estimates

********* Now number of words counts instead of share of words **************
eststo clear
eststo: qui reg pro_worker_count high_pop_pop  i.month foreign anti_pop i.state_num2 ,cluster(state_num2)
eststo: qui reg pro_worker_count high_pop_pop veryclose10 foreign i.month  anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg pro_worker_count high_pop_pop veryclose10 foreign int_exp_close  i.month  anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg pro_worker_count high_pop_pop veryclose10 foreign int_exp_close int_exp_anti i.month  anti_pop  i.state_num2,cluster(state_num2)

eststo: qui reg culture_count high_pop_pop  i.month foreign anti_pop  i.state_num2 ,cluster(state_num2)
eststo: qui reg culture_count high_pop_pop veryclose10 foreign i.month  anti_pop i.state_num2 ,cluster(state_num2)
eststo: qui reg culture_count high_pop_pop veryclose10 int_exp_close foreign i.month  anti_pop  i.state_num2, cluster(state_num2)
eststo: qui reg culture_count high_pop_pop veryclose10 int_exp_close foreign int_exp_anti i.month  anti_pop  i.state_num2,cluster(state_num2)


esttab , replace label se title(Trump's Campaing Strategy: Speeches (Total count) \label {TableTotal})  nomtitle collabels(none) mgroups("Pro-worker Rhetoric (1-4)" "Cultural Rhetoric (5-8)", pattern(1 0 0 0 1 0 0 0)  ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.2f) keep(high_pop* anti* *close* int*)  scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("FE State = *state*" "Foreign = foreign*" "Fe Month =*month") 

esttab using "Table\Trump_text_IVchanged_count.tex", replace label se title(Trump's Campaing Strategy: Speeches (Total count) \label {TableTotal})  nomtitle collabels(none) mgroups("Pro-worker Rhetoric (1-4)" "Cultural Rhetoric (5-8)", pattern(1 0 0 0 1 0 0 0)  ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span)  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.2f) keep(high_pop* anti* *close* int*)  scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("FE State = *state*" "Foreign = foreign*" "FE Month =*month") 


restore

/////
}
}